package com.byx.chatclient.broadcast;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.job.JobScheduler;
import android.app.job.JobService;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Handler;
import android.widget.Toast;

import androidx.core.app.NotificationCompat;

import com.byx.chatclient.R;
import com.byx.chatclient.view.activity.main.MainActivity;

import static android.content.Context.NOTIFICATION_SERVICE;

/**
 * MessageReceivedBroadcast
 *
 * @author Superb
 * @date 2021/2/26 15:36
 */
public class MessageReceivedBroadcast extends BroadcastReceiver {
    /**
     * This method is called when the BroadcastReceiver is receiving an Intent
     * broadcast.  During this time you can use the other methods on
     * BroadcastReceiver to view/modify the current result values.  This method
     * is always called within the main thread of its process, unless you
     * explicitly asked for it to be scheduled on a different thread using
     * {@link Context# registerReceiver(BroadcastReceiver,
     * IntentFilter, String, Handler)}. When it runs on the main
     * thread you should
     * never perform long-running operations in it (there is a timeout of
     * 10 seconds that the system allows before considering the receiver to
     * be blocked and a candidate to be killed). You cannot launch a popup dialog
     * in your implementation of onReceive().
     *
     * <p><b>If this BroadcastReceiver was launched through a &lt;receiver&gt; tag,
     * then the object is no longer alive after returning from this
     * function.</b> This means you should not perform any operations that
     * return a result to you asynchronously. If you need to perform any follow up
     * background work, schedule a {@link JobService} with
     * {@link JobScheduler}.
     * <p>
     * If you wish to interact with a service that is already running and previously
     * bound using {@link Context# bindService(Intent, ServiceConnection, int) bindService()},
     * you can use {@link #peekService}.
     *
     * <p>The Intent filters used in {@link Context#registerReceiver}
     * and in application manifests are <em>not</em> guaranteed to be exclusive. They
     * are hints to the operating system about how to find suitable recipients. It is
     * possible for senders to force delivery to specific recipients, bypassing filter
     * resolution.  For this reason, {@link #onReceive(Context, Intent) onReceive()}
     * implementations should respond only to known actions, ignoring any unexpected
     * Intents that they may receive.
     *
     * @param context The Context in which the receiver is running.
     * @param intent  The Intent being received.
     */
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context,"您有一条新消息",Toast.LENGTH_SHORT).show();
        NotificationManager manager= (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
        /*
        调用NotificationChannel创建通知渠道实例
        并为它设置属性
         */
        //通知渠道的ID
        String id = "channel_01";
        //用户可以看到的通知渠道的名字
        CharSequence name = id;
        //用户可看到的通知描述
        String description = id;
        //构建NotificationChannel实例
        NotificationChannel notificationChannel =
                new NotificationChannel(id,name,NotificationManager.IMPORTANCE_HIGH);
        //配置通知渠道的属性
        notificationChannel.setDescription(description);
        //设置通知出现时的闪光灯
        notificationChannel.enableLights(true);
        notificationChannel.setLightColor(Color.RED);
        //设置通知出现时的震动
        notificationChannel.enableVibration(true);
        notificationChannel.setVibrationPattern(new long[]{100,200,300,400,500,400,300,200,100});
        //在notificationManager中创建通知渠道
        manager.createNotificationChannel(notificationChannel);
        PendingIntent pendingIntent= PendingIntent.getActivity(context,0,
                new Intent(context, MainActivity.class),0);
        Notification notification = new NotificationCompat.Builder(context,id)
                //指定通知的标题内容
                .setContentTitle("通知")
                .setTicker("chat-client")
                //设置通知的内容
                .setContentText("您有一条新消息")
                //指定通知被创建的时间
                .setWhen(System.currentTimeMillis())
                //设置通知的小图标
                .setSmallIcon(R.mipmap.ic_app_logo)
                //设置通知的大图标
                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(),
                        R.mipmap.ic_app_logo))
                //添加点击跳转通知跳转
                .setContentIntent(pendingIntent)
                //实现点击跳转后关闭通知
                .setAutoCancel(true)
                .build();

        /*
        调用NotificationManager的notify()方法将通知显示出来
        传入的第一个参数是通知的id
        传入的第二个参数是notification对象
         */
        manager.notify(1,notification);
    }
}
